//
//  UIStackView_Demo1VC.swift
//  APSwift
//
//  Created by AbnerPei on 2021/11/4.
//

import UIKit
import SnapKit

class UIStackView_Demo1VC: UIViewController {
    
    private var vStackView: UIStackView!
    private var hStackView: UIStackView!

    lazy var lb1: UILabel = {
        let lb = UILabel()
        lb.textAlignment = .center
        lb.font = .systemFont(ofSize: 20)
        lb.text = "第1个Label"
        lb.textColor = .white
        lb.backgroundColor = .lightGray
        return lb
    }()
    
    lazy var lb2: UILabel = {
        let lb = UILabel()
        lb.textAlignment = .center
        lb.font = .systemFont(ofSize: 20)
        lb.text = "第2个Label"
        lb.textColor = .white
        lb.backgroundColor = .red
        return lb
    }()
    
    lazy var lb3: UILabel = {
        let lb = UILabel()
        lb.textAlignment = .center
        lb.font = .systemFont(ofSize: 20)
        lb.text = "第3个Label"
        lb.textColor = .white
        lb.backgroundColor = .blue
        return lb
    }()
    
    lazy var bt1: UIButton = {
        let bt = UIButton(type: .custom)
        bt.backgroundColor = .red
        bt.setTitle("我是一个button", for: .normal)
        bt.addTarget(self, action: #selector(bt1Action(_:)), for: .touchUpInside)
        return bt
    }()
    
    @objc private func bt1Action(_ sender: UIButton) {
        debugPrint("点击了【我是一个button】")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.        
        vStackView = UIStackView()
        vStackView.axis = .vertical
        vStackView.distribution = .equalSpacing
        vStackView.alignment = .center
        vStackView.spacing = 20
        vStackView.backgroundColor = .purple
        view.addSubview(vStackView)
        vStackView.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(220)
            make.right.equalTo(-10)
        }
        
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(didTappedStackView))
        vStackView.addGestureRecognizer(tapGes)
        
        vStackView.addArrangedSubview(lb1)
        vStackView.addArrangedSubview(lb2)
        vStackView.addArrangedSubview(lb3)
        vStackView.addArrangedSubview(bt1)

        lb1.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(60)
        }
        
        lb2.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(80)
        }
        
        lb3.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(100)
        }
        
        bt1.snp.makeConstraints { make in
            make.left.equalTo(50)
            make.right.equalTo(-50)
            make.height.equalTo(100)
        }
        
        let bt = UIButton(type: .custom)
        bt.setTitle("隐藏第2个lbel", for: .normal)
        bt.backgroundColor = .brown
        bt.addTarget(self, action: #selector(didTappedBt(_:)), for: .touchUpInside)
        view.addSubview(bt)
        bt.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 120, height: 50))
            make.top.equalTo(vStackView.snp.bottom).offset(50)
            make.centerX.equalToSuperview()
        }
    }
    
    @objc private func didTappedStackView() {
        debugPrint("点击了stackView")
    }
    
    @objc func didTappedBt(_ bt: UIButton) {
        bt.isSelected = !bt.isSelected
        let str = bt.isSelected ? "显示" : "隐藏"
        bt.setTitle("\(str)第2个lbel", for: .normal)

        UIView.animate(withDuration: 0.25) {
//            self.lb2.isHidden = bt.isSelected
            self.vStackView.spacing = 40
        }
    }

}
